/**
 * udes.ds.rmi
 * SummativeEquation.java
 * 3 sept. 08
 */
package udes.ds.agent;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Stores an equation of the type f(x) + g(x)
 * 
 * @author Luc Bergevin
 * @version 1.0
 */
public class SummativeEquation extends AbstractEquation {

	private static final long serialVersionUID = 1L;
	protected final Equation first;
	protected final Equation second;

	public SummativeEquation() throws NumberFormatException, IOException,
			InstantiationException, IllegalAccessException,
			ClassNotFoundException {
		BufferedReader input = new BufferedReader(new InputStreamReader(
				System.in));
		System.out.println("class_de_l_equation_1:class_de_l_equation_2");
		String[] classnames = input.readLine().split(":");
		first = (Equation) Class.forName(classnames[0]).newInstance();
		second = (Equation) Class.forName(classnames[1]).newInstance();
	}

	public SummativeEquation(Equation first, Equation second) {
		super();
		this.first = first;
		this.second = second;
	}

	public Equation getFirst() {
		return first;
	}

	public Equation getSecond() {
		return second;
	}

	/**
	 * @see udes.ds.rmi.hw.Equation#getFunctionValue(double)
	 */
	@Override
	public double getFunctionValue(double x) {
		return (first.getFunctionValue(x) + second.getFunctionValue(x));
	}

	/**
	 * @see udes.ds.rmi.hw.AbstractEquation#getUserReadableString()
	 */
	@Override
	public String getUserReadableString() {
		return new String("(" + first.getUserReadableString() + ") + ("
				+ second.getUserReadableString() + ")");
	}

	@Override
	public Double[] getNumbers() {
		Double[] fst = first.getNumbers();
		Double[] scd = second.getNumbers();

		Double[] total = new Double[fst.length + scd.length];
		int i = 0;
		for (int j = 0; j < fst.length; j++) {
			total[i] = fst[j];
			i++;
		}
		for (int j = 0; j < scd.length; j++) {
			total[i] = scd[j];
			i++;
		}

		return total;
	}

	@Override
	public String getFingerprint() {
		return first.getFingerprint() + "+" + second.getFingerprint();
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((first == null) ? 0 : first.hashCode());
		result = prime * result + ((second == null) ? 0 : second.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		SummativeEquation other = (SummativeEquation) obj;
		if (first == null) {
			if (other.first != null)
				return false;
		} else if (!first.equals(other.first))
			return false;
		if (second == null) {
			if (other.second != null)
				return false;
		} else if (!second.equals(other.second))
			return false;
		return true;
	}
}
